package com.nineclock.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nineclock.common.constant.SMSConstant;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.utils.BeanHelper;
import com.nineclock.system.dto.SysCompanyUserDTO;
import com.nineclock.system.dto.SysRegisterDTO;
import com.nineclock.system.dto.SysUserDTO;
import com.nineclock.system.mapper.SysCompanyUserMapper;
import com.nineclock.system.mapper.SysUserMapper;
import com.nineclock.system.pojo.SysCompanyUser;
import com.nineclock.system.pojo.SysUser;
import com.nineclock.system.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@Service
@Slf4j
public class SysUserServiceImpl implements SysUserService {


    @Autowired
    SysUserMapper userMapper;

    /**
     * 根据用户名 或者 手机号码 查询用户信息
     * @param username
     * @return
     */
    @Override
    public SysUserDTO queryUser(String username) {

        LambdaQueryWrapper<SysUser> userQw = new LambdaQueryWrapper<>();
        //用户名，或 是手机号都可以
        userQw.eq(SysUser::getUsername, username).or().eq(SysUser::getMobile, username);

        SysUser user = userMapper.selectOne(userQw);
        //转换为DTO数据
        SysUserDTO sysUserDTO = BeanHelper.copyProperties(user, SysUserDTO.class);

        return sysUserDTO;
    }

    @Autowired
    SysCompanyUserMapper companyUserMapper;

    @Override
    public SysCompanyUserDTO findById(Long userId) {
        SysCompanyUser companyUser = companyUserMapper.selectById(userId);
        if(companyUser != null){
            return BeanHelper.copyProperties(companyUser, SysCompanyUserDTO.class);
        }
        return null;
    }
    @Autowired
    RedisTemplate redisTemplate;
    @Override
    public Long register(SysRegisterDTO sysRegisterDTO) {
        if(sysRegisterDTO == null
                || sysRegisterDTO.getMobile() == null
                || sysRegisterDTO.getCheckcode() == null
                || sysRegisterDTO.getPassword() == null){
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }

        //获取redis验证码
        Object realCode = redisTemplate.boundValueOps(SMSConstant.SMS_TYPE_REGISTER + sysRegisterDTO.getMobile()).get();
        //校验验证码
        if(realCode == null || ! realCode.equals(sysRegisterDTO.getCheckcode())){
            throw new NcException(ResponseEnum.INVALID_VERIFY_CODE);
        }


        //注册用户
        SysUser user = new SysUser();
        //设置信息
        user.setMobile(sysRegisterDTO.getMobile());
        user.setPassword(new BCryptPasswordEncoder().encode(sysRegisterDTO.getPassword()));
        user.setStatus((short)1);
        user.setCreateTime(new Date());

        userMapper.insert(user);

        return user.getId();
    }
}
